# -*- coding: utf-8 -*-


from job51.settings import logger
import json, time
from job51.other.models import session, jobs, job_detail
from job51.items import *

class TutorialPipeline(object):
    def process_item(self, item, spider):
        return item


class job51Pipeline(object):
    def open_spider(self, spider):
        self.file = open('output.log', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        logger.info('myprint process_item')

        add_time = time.strftime('%Y-%m-%d %H:%M:%S')
        if isinstance(item, jobList):
            # 写入文本
            # page = item['page']
            # item.pop('page')
            # context = json.dumps(dict(item),ensure_ascii=False) + '第' + str(page) + '页' + '\n'
            # context = json.dumps(dict(item),ensure_ascii=False) + '\n'
            # self.file.write(context)

            # 写入mysql
            r_jobid = item['r_jobid']
            row = session.query(jobs).filter_by(r_jobid=r_jobid).first()
            if not getattr(row, 'r_jobid', None):
                obj = jobs(station=item['station'],r_jobid=item['r_jobid'],job_detail_url=item['job_detail_url'],company=item['company'],address=item['address'],salary=item['salary'],release_time=item['release_time'],page=item['page'], add_time=add_time)
                session.add(obj)
                session.commit()
            else:
                print(row.company + '---列表页---已存在')
        if isinstance(item, jobDetail):
            # 写入mysql
            r_jobid = item['r_jobid']
            row_i = session.query(jobs).filter_by(r_jobid=r_jobid).first()
            deta = session.query(job_detail).filter_by(r_jobid=r_jobid).first()
            logger.info('row_i')
            # logger.info(dir(row_i))
            if getattr(row_i, 'r_jobid', None) and not getattr(deta, 'r_jobid', None):
                obj = job_detail(station=item['station'],company=item['company'],r_jobid=r_jobid,l_jobid=row_i.id,address=item['address'],experience=item['experience'],xueli=item['xueli'],people_num=item['people_num'],release_time=item['release_time'],tags=item['tags'],job_info=item['job_info'],contact_way=item['contact_way'],company_info=item['company_info'],add_time=add_time)
                session.add(obj)
                session.commit()
            else:
                print(row_i.company + '---详情页---已存在')

            # l_jobid = 0
            # # logger.info(item)
            # obj = job_detail(station=item['station'],company=item['company'],l_jobid=l_jobid,msg_1=item['msg_1'],tags=item['tags'],job_info=item['job_info'],contact_way=item['contact_way'],company_info=item['company_info'],add_time=add_time)
            # session.add(obj)
            # session.commit()

    def close_spider(self,spider):
        # session.commit()
        self.file.close()